<?
/**	Sphinx Search Class
*	Date:	2011-10-12
*	Author:	terrywu
*/

class Sphinx{	// class start

	private $sphinx;
	private $datasize;

	/** 初始化
	* @param Array $config 配置 格式: array('ip','port','timeout','datasize')
	*/
	public function __construct($config=array()){

		require_once(LIB_PATH.'/Vendor/Sphinx/sphinxapi.php');
		
		global $sphinxConfig;
		
		$server = isset($config['ip'])? $config['ip'] : $sphinxConfig['ip'];
		$port = isset($config['port'])? $config['port'] : $sphinxConfig['port'];
		$timeout = isset($config['timeout'])? $config['timeout'] : $sphinxConfig['timeout'];
		$datasize = isset($config['datasize'])? $config['datasize'] : $sphinxConfig['datasize'];
		$this->datasize = $datasize;

		$this->sphinx = new SphinxClient();
		$this->sphinx->SetServer($server, $port);
		$this->sphinx->SetConnectTimeout($timeout);
		$this->sphinx->SetArrayResult(true);
		$this->sphinx->SetMatchMode(SPH_MATCH_PHRASE);
		$this->sphinx->SetLimits(0, $this->datasize);
	}


	/** 獲取搜尋結果
	* @param	Mixed	$keyword	搜尋的關鍵字 String or Array
	* @param	String	$source		配置源，如多個用英文,分隔
	* @param	Array	$param		array(
	*									'filter'	=>	'',	搜尋條件	Array
	*									'weight'	=>	'',	搜尋比重	Array
	*									'groupby'	=>	'',	篩選重複	String or Array
	*									'sort'		=>	'',	排序		String or Array
	*									'limit'		=>	''	取記錄數	String or Array	
	*								);
	* @return	Array
	*/
	public function getSphinx($keyword, $source, $param=array()){
		if(empty($keyword) || empty($source)){
			return false;
		}

		$filter = isset($param['filter']) && is_array($param['filter'])? $param['filter'] : array(); 
		$weight = isset($param['weight']) && is_array($param['weight'])? $param['weight'] : array();
		$groupby = isset($param['groupby'])? $param['groupby'] : '';
		$sort = isset($param['sort'])? $param['sort'] : '';
		$limit = isset($param['limit'])? $param['limit'] : '';

		if($filter){
			foreach($filter as $key=>$val){
				$this->sphinx->SetFilter($key, $val);
			}
		}

		if($weight){
			$this->sphinx->SetFieldWeights($weight);
		}

		if($groupby){
			if(is_array($groupby)){
				if(count($groupby)==3){
					$this->sphinx->SetGroupBy($groupby[0], $groupby[1], $groupby[2]);
				}
			}else{
				$this->sphinx->SetGroupBy($groupby, SPH_GROUPBY_ATTR, '@weight DESC');
			}
		}

		if($sort){
			if(is_array($sort)){
				if(count($sort)==2){
					$this->sphinx->SetSortMode($sort[0], $sort[1]);
				}
			}else{
				$this->sphinx->SetSortMode(SPH_SORT_RELEVANCE, $sort);
			}
		}

		if($limit){
			if(is_array($limit)){
				if(count($limit)==2){
					$this->sphinx->SetLimits($limit[0], $limit[1]);
				}
			}else{
				$this->sphinx->SetLimits(0, $limit);
			}
		}else{
			$this->sphinx->SetLimits(0, $this->datasize);
		}

		if(is_array($keyword)){
			foreach($keyword as $key){
				$this->sphinx->AddQuery($key, $source);
			}
		}else{
			$this->sphinx->AddQuery($keyword, $source);
		}

		$result = $this->sphinx->RunQueries();

		$this->sphinx->ResetFilters();
		$this->sphinx->ResetGroupBy();

		return $result;
	}

}	// class end
?>